package teste;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class ModificadorTxtVersao2 {
	
	private static String nomeArquivo;
	
	public static String modificaArquivo(int coluna, String modificacao) throws IOException {
		
		FileInputStream inputStream = new FileInputStream(nomeArquivo);
		BufferedInputStream buffer = new BufferedInputStream(inputStream);
		
        String saida = "";
        
        try {
        	int read = buffer.read();
        	int cont = 1;
        	char quebraLinha = '\n';
        	
        	int teste = 0;
        	
			while(read != -1) {
				char caracter = (char) (read);
				if (caracter == quebraLinha) {
					cont = 0;
					teste++;
				}
				if (cont == coluna) {
					saida += modificacao;
				}
				saida += caracter;
				read = buffer.read();
				cont++;
				
				System.out.println(teste);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		
		} finally {
			if (inputStream != null)
					inputStream.close();
		 }
        
        System.out.println(" ----> Done");
		return saida;
	}
	
	public static void geraSaida(String arquivoFormatado) {

		try {
			FileWriter fstream = new FileWriter(nomeArquivo);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(arquivoFormatado);
			out.close();
		} catch (Exception e) {
			System.err.println("Erro: " + e.getMessage());
		}
	}
	
	public static void main(String[] args) {
		try {
			nomeArquivo = "023002022011LancamentoContabilItem.txt";
			String arquivoFormatado = modificaArquivo(7, "000000000000");
			geraSaida(arquivoFormatado);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
